package com.ser02;

import com.pojo.User;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@WebServlet("/ServletTest6")
public class ServletTest6 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //避免编码不一致
        request.setCharacterEncoding("utf-8");

        /*//创建user对象
        User user = new User();
        System.out.println(user);

        user.setUsername(request.getParameter("username"));
        user.setPassword(request.getParameter("password"));
        user.setAge(Integer.parseInt(request.getParameter("age")));
        user.setSex(request.getParameter("sex"));
        user.setPhone(request.getParameter("phone"));
        user.setEmail(request.getParameter("email"));
        System.out.println(user);*/

        //创建user对象
        User user = new User();
        System.out.println(user);

        //获取数据
        Map<String, String[]> map = request.getParameterMap();

        try {
            //数据封装
            BeanUtils.populate(user, map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(user);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
